package server

import (
	"blog/app/model"
	"blog/app/utils"
	"blog/app/common"
)

type ConfigServer struct {
}

type ConfigData struct {
	Name      string `json:"name"`
	Signature string `json:"signature"`
	Contact   string `json:"contact"`
	Bg        string `json:"bg"`
}

var Info  *model.Config

func (*ConfigServer) Get() *ConfigData {
	c := &model.Config{}
	Info = c.Get()
	if Info == nil {
		return nil
	}
	cd := &ConfigData{
		Name:      Info.Name,
		Signature: Info.Signature,
		Contact:   Info.Contact,
		Bg:        Info.Bg,
	}
	return cd
}

func (*ConfigServer) Update(c *model.Config) *ConfigData {
	Info = c.UpdateOne()
	if Info == nil {
		return nil
	}
	cd := &ConfigData{
		Name:      Info.Name,
		Signature: Info.Signature,
		Contact:   Info.Contact,
		Bg:        Info.Bg,
	}
	return cd
}

// 登陆返回一个token，这里简单点就是一个随机字符串
func (c *ConfigServer) Login(pass string) string {
	common.Token = utils.GetRandomString(32)
	if Info == nil{
		c.Get()
	}
	if utils.Md5(pass) == Info.Password {
		common.Token = utils.GetRandomString(32)
		return common.Token
	}
	return ""
}
